﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MiniExcelLibs;

namespace BeiJingQuestion2
{
    public class demo1
    {
        public static void Main(string[] args)
        {
            // 提示用户输入Excel文件路径
            //Console.WriteLine("请输入Excel文件路径（例如：C:\\Data.xlsx）：");
            //输出当前路径
            string currentDirectory = Directory.GetCurrentDirectory();
            Console.WriteLine($"当前路径: {currentDirectory}");

            string filePath = "..\\..\\..\\Excel\\Data.xlsx";

            if (!File.Exists(filePath))
            {
                Console.WriteLine("文件不存在！");
                return;
            }

            Dictionary<string, IList<string>> records = new Dictionary<string, IList<string>>();
            try
            {
                // 通过MiniExcel.Query读取Excel数据
                // Query返回 IEnumerable<dynamic>，这里转换为字典形式，方便后续按列名查询
                var data = MiniExcel.Query(filePath);
                int c = 0;
                foreach (var o in data)
                {
                    if (c == 0)
                    {
                        c++;
                        continue;
                    }
                    //我希望获取o的值
                    var row = o as IDictionary<string, object>;
                    string key = null;
                    List<string> value = new List<string> { };
                    if (row != null)
                    {
                        // 遍历字典，输出每个键值对
                        foreach (var kv in row)
                        {
                            //Console.WriteLine($"{kv.Key} = {kv.Value}");
                            if (kv.Key.Equals("B"))
                            {
                                key = (string)kv.Value;
                            }
                            else
                            {
                                value.Add(kv.Value.ToString());
                            }
                        }
                    }

                    records.Add(key, value);

                    //Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"解析Excel文件时出错：{ex.Message}");
                return;
            }

            Console.WriteLine("Excel数据解析完成。");

            // 循环让用户输入Name进行查询
            while (true)
            {
                Console.WriteLine("请输入Name（输入exit退出）：");
                string inputName = Console.ReadLine();
                if (inputName.Trim().Equals("exit", StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                if (records.ContainsKey(inputName))
                {
                    var value = records[inputName];

                    Console.WriteLine($"Layer:{value[0]},Param Size(bit):{value[1]}");
                }
            }
        }
    }
}
